package cn.shenlong.portal.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shenlong.common.Constants;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Content;
import cn.shenlong.portal.service.ChannelService;
import cn.shenlong.portal.service.ContentService;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/content/admin")
public class ContentAController {

	@Autowired
	private ContentService contentService;

	@Autowired
	private ChannelService channelService;

	@RequestMapping("/index")
	public String index() {
		return "/server/content/index";
	}

	@RequestMapping("/lefttree")
	public String lefttree() {
		return "/server/content/lefttree";
	}

	@RequestMapping("/list")
	public String list(Content content, Long channelId, Date beginTime,
			Date endTime, Long queryOrderBy, Integer pageNo, Integer pageSize,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		if (queryOrderBy == null) {
			queryOrderBy = 1l;
		}
		Map<String, Object> otherParm = new HashMap<String, Object>();
		otherParm.put("beginTime", beginTime);
		otherParm.put("endTime", endTime);
		otherParm.put("queryOrderBy", queryOrderBy);
		otherParm.put("channelId", channelId);
		Pagination p = contentService.findContents(content, otherParm,
				pageSize, pageNo);
		model.put("pagination", p);
		model.put("beginTime", beginTime);
		model.put("endTime", endTime);
		model.put("queryOrderBy", queryOrderBy);
		model.put("channelId", channelId);
		return "/server/content/list";
	}

	@RequestMapping("/addContent")
	public String addContent(Long channelId, HttpServletRequest request,
			ModelMap model) {
		model.put("channel", channelService.findByPK(channelId));

		return "/server/content/add";
	}

	@RequestMapping("/saveContent")
	public String saveContent(Content content, Long channelId,
			HttpServletRequest request, ModelMap model) {
		try {
			if (content.getCreateDate() == null) {
				content.setCreateDate(new Date());
			}
			if (content.getPriority() == null) {
				content.setPriority(0l);
			}
			if (StringUtils.isBlank(content.getLink())) {
				content.setHasLink(0l);
				content.setLink(null);
			} else {
				content.setHasLink(1l);
			}
			contentService.save(content, channelId);
			model.put("result", true);
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", e.getMessage());
		}

		return addContent(channelId, request, model);
	}

	@RequestMapping("/editContent")
	public String editContent(Long contentId, Long channelId,
			HttpServletRequest request, ModelMap model) {
		Content content = contentService.findContentByContentId(contentId);
		model.put("channel", channelService.findByPK(channelId));
		model.put("content", content);
		return "/server/content/edit";
	}

	@RequestMapping("/updateContent")
	public String updateContent(Content content, Long channelId,
			HttpServletRequest request, ModelMap model) {
		try {
			if (content.getCreateDate() == null) {
				content.setCreateDate(new Date());
			}
			if (content.getPriority() == null) {
				content.setPriority(0l);
			}
			content.setUpdateDate(new Date());
			if (StringUtils.isBlank(content.getLink())) {
				content.setHasLink(0l);
				content.setLink(null);
			} else {
				content.setHasLink(1l);
			}
			contentService.updateContent(content);
			model.put("result", true);
		} catch (Exception e) {
			e.printStackTrace();
			model.put("result", e.getMessage());
		}

		return editContent(content.getContentId(), channelId, request, model);
	}

	@RequestMapping("deleteContent")
	@ResponseBody
	public Map<String, Object> deleteContent(Long channelId, Long contentId,
			String contentIdStr, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (contentId == null) {

				String[] tmp = contentIdStr.split(",");
				List<Long> contentIds = new ArrayList<Long>();
				for (int i = 0; i < tmp.length; i++) {
					contentIds.add(Long.parseLong(tmp[i]));
				}
				contentService.deleteByContentIdAndChannelId(contentIds,
						channelId);
			} else {
				List<Long> contentIds = new ArrayList<Long>();
				contentIds.add(contentId);
				contentService.deleteByContentIdAndChannelId(contentIds,
						channelId);
			}
			resultMap.put("result", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}

	@RequestMapping("uploadImage")
	@ResponseBody
	public Map<String, Object> uploadImage(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(
					request, Constants.CONTENT_IMG_URI);
			resultMap.put("path", resultList.get(0).get("imagePath"));
			resultMap.put("result", true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", false);
		}

		return resultMap;
	}

}
